using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;

namespace EntLibContrib.PolicyInjection
{
    [Assembler(typeof(OrMatchingRuleAssembler))]
    public class OrMatchingRuleData : CompositeMatchingRuleData
    {
        public OrMatchingRuleData(string name)
            : base(name, typeof(OrMatchingRule))
        {
        }

        public OrMatchingRuleData()
        {
        }
    }

    internal class OrMatchingRuleAssembler : IAssembler<IMatchingRule, MatchingRuleData>
    {
        public IMatchingRule Assemble(IBuilderContext context, MatchingRuleData objectConfiguration,
            IConfigurationSource configurationSource,
            ConfigurationReflectionCache reflectionCache)
        {
            OrMatchingRuleData ruleData = (OrMatchingRuleData)objectConfiguration;
            List<IMatchingRule> rules = new List<IMatchingRule>();
            foreach (MatchingRuleData matchingRuleData in ruleData.MatchingRules)
            {
                IMatchingRule matchingRule = MatchingRuleCustomFactory.Instance.Create(context, matchingRuleData, configurationSource, reflectionCache);
                rules.Add(matchingRule);
            }

            return new OrMatchingRule(rules);
        }
    }
}
